import {request} from "../utils/request";
import {AlbumBrief, AlbumCreator, Singer, Song} from "./types";

function songListOfAlbum(id: string): Promise<Song[]> {
  return request.fetch({ url: '/songlist', params: { id } }).then((res: any) => res.data.songlist) as unknown as Promise<Song[]>;
}

function similarSongs(id: number): Promise<Song[]> {
  return request
    .fetch({ url: '/song/similar', params: { id } })
    .then((res: any) => {
      return res.data.map((item: any) => {
        return {
          songid: item.id,
          songmid: item.mid,
          songname: item.title,
          imgurl: 'https://y.gtimg.cn/music/photo_new/T002R150x150M000002yiKkB2iv3Cp_1.jpg?max_age=2592000',
          singer: item.singer,
          singerStr: item.singer.map((singer: Singer) => singer.name).join('/')
        }
      });
    }) as unknown as Promise<Song[]>;
}

function similarAlbums(id: number): Promise<AlbumBrief[]> {
  return request
    .fetch({ url: '/song/playlist', params: { id } })
    .then((res: any) => {
      return res.data.map((item: any) => {
        return {
          dissid: item.tid,
          dissname: item.dissname,
          imgurl: item.imgurl,
          listennum: item.listen_num,
          listennumStr: item.listen_num_str,
          creator: {
            name: item.creator,
            avatarUrl: '',
            isVip: 0
          }
        }
      });
    }) as unknown as Promise<AlbumBrief[]>;
}

export { songListOfAlbum, similarSongs, similarAlbums };
